<!DOCTYPE html>
<html>
<head>
<title>DAY10</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) */
/* Author: Nicolas Hery - http://nicolashery.com */
/* Version: b13fe65ca28d2e568c6ed5d7f06581183df8f2ff */
/* Source: https://github.com/nicolahery/markdownpad-github */

/* RESET
=============================================================================*/

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
}

/* BODY
=============================================================================*/

body {
  font-family: Helvetica, arial, freesans, clean, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  background-color: #fff;
  padding: 20px;
  max-width: 960px;
  margin: 0 auto;
}

body>*:first-child {
  margin-top: 0 !important;
}

body>*:last-child {
  margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
  margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
  margin: 20px 0 10px;
  padding: 0;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
  font-size: inherit;
}

h1 {
  font-size: 28px;
  color: #000;
}

h2 {
  font-size: 24px;
  border-bottom: 1px solid #ccc;
  color: #000;
}

h3 {
  font-size: 18px;
}

h4 {
  font-size: 16px;
}

h5 {
  font-size: 14px;
}

h6 {
  color: #777;
  font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
  margin-top: 0;
  padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
  margin-top: 0;
  padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
  margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
  color: #4183C4;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
  padding-left: 30px;
}

ul li > :first-child, 
ol li > :first-child, 
ul li ul:first-of-type, 
ol li ol:first-of-type, 
ul li ol:first-of-type, 
ol li ul:first-of-type {
  margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
  margin-bottom: 0;
}

dl {
  padding: 0;
}

dl dt {
  font-size: 14px;
  font-weight: bold;
  font-style: italic;
  padding: 0;
  margin: 15px 0 5px;
}

dl dt:first-child {
  padding: 0;
}

dl dt>:first-child {
  margin-top: 0px;
}

dl dt>:last-child {
  margin-bottom: 0px;
}

dl dd {
  margin: 0 0 15px;
  padding: 0 15px;
}

dl dd>:first-child {
  margin-top: 0px;
}

dl dd>:last-child {
  margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
  font-size: 12px;
  font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
  margin: 0 0px;
  padding: 0px 0px;
  white-space: nowrap;
  border: 1px solid #eaeaea;
  background-color: #f8f8f8;
  border-radius: 3px;
}

pre>code {
  margin: 0;
  padding: 0;
  white-space: pre;
  border: none;
  background: transparent;
}

pre {
  background-color: #f8f8f8;
  border: 1px solid #ccc;
  font-size: 13px;
  line-height: 19px;
  overflow: auto;
  padding: 6px 10px;
  border-radius: 3px;
}

pre code, pre tt {
  background-color: transparent;
  border: none;
}

kbd {
    -moz-border-bottom-colors: none;
    -moz-border-left-colors: none;
    -moz-border-right-colors: none;
    -moz-border-top-colors: none;
    background-color: #DDDDDD;
    background-image: linear-gradient(#F1F1F1, #DDDDDD);
    background-repeat: repeat-x;
    border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
    border-image: none;
    border-radius: 2px 2px 2px 2px;
    border-style: solid;
    border-width: 1px;
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    line-height: 10px;
    padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
  border-left: 4px solid #DDD;
  padding: 0 15px;
  color: #777;
}

blockquote>:first-child {
  margin-top: 0px;
}

blockquote>:last-child {
  margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
  clear: both;
  margin: 15px 0;
  height: 0px;
  overflow: hidden;
  border: none;
  background: transparent;
  border-bottom: 4px solid #ddd;
  padding: 0;
}

/* TABLES
=============================================================================*/

table th {
  font-weight: bold;
}

table th, table td {
  border: 1px solid #ccc;
  padding: 6px 13px;
}

table tr {
  border-top: 1px solid #ccc;
  background-color: #fff;
}

table tr:nth-child(2n) {
  background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
  max-width: 100%
}
</style>
</head>
<body>
<h3>46. 收货地址-删除-控制器层</h3>
<p><strong>a. 处理异常</strong></p>
<p>需要处理<code>DeleteException</code></p>
<p><strong>b. 设计请求</strong></p>
<pre><code>请求路径：/addresses/{aid}/delete
请求参数：@PathVariable(&quot;aid&quot;) Integer aid, HttpSession session
请求类型：POST
响应数据：JsonResult&lt;Void&gt;
</code></pre>

<p><strong>c. 处理请求</strong></p>
<pre><code>@RequestMapping(&quot;{aid}/delete&quot;)
public JsonResult&lt;Void&gt; delete(
    @PathVariable(&quot;aid&quot;) Integer aid,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            HttpSession session) {
    // 从Session中获取uid和username
    Integer uid = getUidFromSession(session);
    String username = getUsernameFromSession(session);
    // 调用业务层对象执行设置默认
    addressService.delete(aid, uid, username);
    // 响应成功
    return new JsonResult&lt;&gt;(SUCCESS); 
}
</code></pre>

<h3>47. 收货地址-删除-前端页面</h3>
<h3>48. 主页-显示热销排行列表-实体类</h3>
<p>创建<code>cn.tedu.store.entity.Goods</code>商品数据的实体类，继承自<code>BaseEntity</code>：</p>
<h3>49. 主页-显示热销排行列表-持久层</h3>
<p>查询4条热销商品的SQL语句大致是：</p>
<pre><code>select * from t_goods where state=1 and num&gt;100 order by priority desc limit 0,4;
</code></pre>

<p>创建<code>cn.tedu.store.mapper.GoodsMapper</code>处理商品数据的持久层接口，并添加抽象方法：</p>
<pre><code>List&lt;Goods&gt; findHotList();
</code></pre>

<p>然后复制得到<code>GoodsMapper.xml</code>文件，修改根节点的<code>namespace</code>对应以上接口，然后配置以上方法的映射：</p>
<pre><code>&lt;mapper namespace=&quot;cn.tedu.store.mapper.GoodsMapper&quot;&gt;

    &lt;!-- 获取热销商品列表 --&gt;
    &lt;!-- List&lt;Goods&gt; findHotList() --&gt;
    &lt;select id=&quot;findHotList&quot;
        resultType=&quot;cn.tedu.store.entity.Goods&quot;&gt;
        SELECT
            id, title,
            price, image
        FROM
            t_goods
        WHERE
            status=1 AND num&gt;100
        ORDER BY
            priority DESC
        LIMIT 0,4
    &lt;/select&gt;

&lt;/mapper&gt;
</code></pre>

<p>最后，在<strong>src/test/java</strong>下创建<code>cn.tedu.store.mapper.GoodsMapperTests</code>测试类，编写并执行单元测试：</p>
<pre><code>@RunWith(SpringRunner.class)
@SpringBootTest
public class GoodsMapperTests {

    @Autowired
    private GoodsMapper mapper;

    @Test
    public void findHotList() {
        List&lt;Goods&gt; list = mapper.findHotList();
        System.err.println(&quot;BEGIN:&quot;);
        for (Goods item : list) {
            System.err.println(item);
        }
        System.err.println(&quot;END.&quot;);
    }

}
</code></pre>

<h3>50. 主页-显示热销排行列表-业务层</h3>
<p>查询并显示列表类的操作没有异常！</p>
<p>创建<code>cn.tedu.store.service.IGoodsService</code>业务层接口，并添加抽象方法：</p>
<pre><code>/**
 * 获取热销商品列表
 * @return 热销商品列表
 */
List&lt;Goods&gt; getHotList();
</code></pre>

<p>然后，创建<code>cn.tedu.store.service.impl.GoodsServiceImpl</code>业务层实现类，实现以上接口，添加<code>@Service</code>注解，并在类中添加声明<code>@Autowired private GoodsMapper goodsMapper;</code>持久层对象：</p>
<pre><code>@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

}
</code></pre>

<p>在实现时，先将持久层的方法再次复制过来，并私有化实现：</p>
<pre><code>/**
 * 获取热销商品列表
 * @return 热销商品列表
 */
private List&lt;Goods&gt; findHotList() {
    return goodsMapper.findHotList();
}
</code></pre>

<p>然后实现接口中抽象方法，调用以上私有方法即可实现：</p>
<pre><code>@Override
public List&lt;Goods&gt; getHotList() {
    return findHotList();
}
</code></pre>

<p>在<strong>src/test/java</strong>下创建<code>cn.tedu.store.service.GoodsServiceTests</code>单元测试类，编写并执行单元测试：</p>
<pre><code>@RunWith(SpringRunner.class)
@SpringBootTest
public class GoodsServiceTests {

    @Autowired
    private IGoodsService service;

    @Test
    public void getHotList() {
        List&lt;Goods&gt; list = service.getHotList();
        System.err.println(&quot;BEGIN:&quot;);
        for (Goods item : list) {
            System.err.println(item);
        }
        System.err.println(&quot;END.&quot;);
    }

}
</code></pre>

<h3>51. 主页-显示热销排行列表-控制器</h3>
<p>此次不需要添加对异常的处理！</p>
<p>此次需要新创建<code>cn.tedu.store.controller.GoodsController</code>控制器类，继承自<code>BaseController</code>，添加<code>@RestController</code>和<code>@RequestMapping(&quot;goods&quot;)</code>注解，并在控制器类中声明<code>@Autowired private IGoodsService goodsService;</code>业务层对象：</p>
<pre><code>@RestController
@RequestMapping(&quot;goods&quot;)
public class GoodsController extends BaseController {

    @Autowired
    private IGoodsService goodsService;

}
</code></pre>

<p>然后，在控制器类中添加处理请求的方法：</p>
<pre><code>@GetMapping(&quot;hot&quot;)
public JsonResult&lt;List&lt;Goods&gt;&gt; getHotList() {
    // 调用业务层对象获取数据
    // 返回
}
</code></pre>

<p>关于商品数据的访问，都应该是不要求登录的，则应该在登录拦截器的配置中添加白名单：<code>/goods/**</code>、<code>/web/index.html</code>、<code>/web/product.html</code>。</p>
<p>全部完成后，可通过<code>http://localhost:8080/goods/hot</code>进行测试访问。</p>
<h3>51. 主页-显示热销排行列表-前端界面</h3>
<h3>53. 显示商品详情-持久层</h3>
<p>显示商品详情的SQL语句大致是：</p>
<pre><code>select title,sell_point,price,image,status,num from t_goods where id=?
</code></pre>

<p>然后在接口中添加抽象方法：</p>
<pre><code>Goods findById(Long id);
</code></pre>

<p>然后配置该抽象方法的映射：</p>
<pre><code>&lt;!-- 根据商品id查询商品详情 --&gt;
&lt;!-- Goods findById(Long id) --&gt;
&lt;select id=&quot;findById&quot;
    resultType=&quot;cn.tedu.store.entity.Goods&quot;&gt;
    SELECT
        title,
        sell_point AS sellPoint,
        price, image,
        status, num
    FROM
        t_goods
    WHERE
        id=#{id}
&lt;/select&gt;
</code></pre>

<p>最后，编写并执行单元测试：</p>
<pre><code>@Test
public void findById() {
    Long id = 10000042L;
    Goods result = mapper.findById(id);
    System.err.println(result);
}
</code></pre>

<h3>54. 显示商品详情-业务层</h3>
<p>查询操作可以不涉及异常。</p>
<p>在<code>IGoodsService</code>中添加抽象方法：</p>
<pre><code>Goods getById(Long id);
</code></pre>

<p>然后在<code>GoodsServiceImpl</code>中粘贴持久层的抽象方法，将其私有化实现：</p>
<pre><code>/**
 * 根据商品id查询商品详情
 * @param id 商品id
 * @return 匹配的商品详情，如果没有匹配的数据，则返回null
 */
private Goods findById(Long id) {
    return goodsMapper.findById(id);
}
</code></pre>

<p>再重写接口中的抽象方法，调用自身的私有方法实现功能：</p>
<pre><code>@Override
public Goods getById(Long id) {
    return findById(id);
}
</code></pre>

<p>最后，编写并执行单元测试：</p>
<pre><code>@Test
public void getById() {
    Long id = 10000042L;
    Goods result = service.getById(id);
    System.err.println(result);
}
</code></pre>

<h3>55. 显示商品详情-控制器层</h3>
<p>由于业务层没有抛出新的异常，则控制器层也不需要处理异常。</p>
<p>需要在<code>GoodsController</code>中添加处理请求的方法：</p>
<pre><code>@GetMapping(&quot;{id}/details&quot;)
public JsonResult&lt;Goods&gt; getById(
    @PathVariable(&quot;id&quot;) Long id) {
    // 调用业务层对象获取数据
    Goods data = goodsService.getById(id);
    // 返回
    return new JsonResult&lt;&gt;(SUCCESS, data);
}
</code></pre>

<p>完成后，通过<code>http://localhost:8080/goods/10000042/details</code>进行测试。</p>
<h3>-----------------------------------------------</h3>
<h3>附1：Eclipse常用快捷键</h3>
<pre><code>代码提示：Alt + /
添加或移除单行注释：Ctrl + /
换行：Shift + Enter
解决方案：Ctrl + 1
格式化代码：Ctrl + Shift + F
整理导包语句：Ctrl + Shift + O
代码整行移动：Alt + 上/下
复制整行代码：Ctrl + Alt + 上/下
删除整行代码：Ctrl + D
定位光标到行首/行尾：Home/End
定位光标到文首/文尾：Ctrl + Home/End
文档内重命名：选中后，Ctrl + 2, R
</code></pre>


</body>
</html>
<!-- This document was created with MarkdownPad, the Markdown editor for Windows (http://markdownpad.com) -->
